﻿#light

open System.Net
open System.IO

type System.Net.WebRequest with
  member x.GetResponseAsync() =
    Async.BuildPrimitive(x.BeginGetResponse, x.EndGetResponse)
    
let downloadPage(url:string) = 
  async { let request = WebRequest.Create(url) 
          use! response = request.AsyncGetResponse()
          use stream = response.GetResponseStream() 
          use reader = new StreamReader(stream) 
          let! html = reader.AsyncReadToEnd() 
          return (url, html)
        }

let download(arr : ResizeArray<string>) =      
    let urls = List.of_seq(arr)
    let dotnetresult = new System.Collections.Generic.Dictionary<string,string>()
    let results = Async.Run(Async.Parallel [for site in urls -> downloadPage site]) 
    results |> Seq.iter(fun x -> dotnetresult.Add( (fst x) , (snd x)))
    dotnetresult

